<?php

class Roles extends HTech_Db_Table
{
    protected $_name    = 'view_roles';
    protected $_primary = 'RoleId';
    
    protected $_dependentTables = array('Users', 'LnkResourcesRoles', 'LnkAlertsRoles');
    protected $_lnkTables = array('LnkResourcesRoles' => 'ResourceId', 'LnkAlertsRoles' => 'AlertId');
    
    protected function _setGridMetadata()
    {
        $this->_gridMetadata['title'] = $this->translate("Roles");
        $this->_gridMetadata['onClick'] = 'admin.role.view';
        $this->_gridMetadata['headers'] = array
            (
                array('RoleId',            50, $this->translate("Id"),             'String'),
                array('Role',             200, $this->translate("Role"),           'String'),
                array('Comments',         300, $this->translate("Comments"),       'String'),
                array('LastEditDate',     150, $this->translate("Last edit date"), 'DateTime', 'Date'),
                array('LastEditFullName', 150, $this->translate("Last edit user"), 'String')
            );
        return $this;
    }
    
    protected function _setRecordDependentRows()
    {
        $this->_record->r = (object) array('resources' => null, 'users' => null);
        $this->_record->r->resources = $this->_record->d->findManyToManyRowset('Resources','LnkResourcesRoles');
        array_walk($this->_record->r->resources->toArray(), array($this, '_setRecordLnkData'), array('LnkResourcesRoles', 'ResourceId')); 
        $this->_record->r->alerts = $this->_record->d->findManyToManyRowset('Alerts','LnkAlertsRoles'); 
        array_walk($this->_record->r->alerts->toArray(), array($this, '_setRecordLnkData'), array('LnkAlertsRoles', 'AlertId')); 
        $this->_record->r->users = $this->_record->d->findDependentRowset('Users');        
    }
    
    /*
    static function getRoleOptions()
    {
        return Zend_Registry::get('db')->fetchPairs("select RoleId, Role from roles");
    }
     * 
     */
}